package com.sunzr0119.conventioncentertemplatebackend.controller;


import com.sunzr0119.conventioncentertemplatebackend.service.CaptchaService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/captcha-api")
public class CaptchaController {

    @Resource
    private CaptchaService captchaService;

    /**
     * 获取验证码图片（Base64格式）
     */
    @GetMapping("/getImgCaptcha")
    public Map<String, String> getCaptcha() {
        return captchaService.generateImgCaptcha();
    }

    @PostMapping("/sendEmailCaptcha/{toEmail}")
    public ResponseEntity<?> sendEmailCaptcha(@PathVariable String toEmail) {
        // 生成随机验证码（6 位数字）
        String captcha = String.format("%06d", new Random().nextInt(999999));

        // 调用邮件服务发送验证码
        ResponseEntity<String> success = captchaService.sendEmailCaptcha(toEmail, captcha);

        if (success.getStatusCode().equals(HttpStatus.OK)) {
            return ResponseEntity.ok(Collections.singletonMap("message", "验证码已发送！"+success.getBody()));
        } else {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Collections.singletonMap("message", "验证码发送失败"));
        }
    }
}
